(function(app){
	app
		.controller("presentdeliverCtrl",["$scope","myFactory",'$http','$state','$stateParams','$location','$ionicHistory','$timeout','$ionicLoading','$rootScope',function($scope,myFactory,$http,$state,$stateParams,$location,$ionicHistory,$timeout,$ionicLoading,$rootScope){
			var login = $stateParams.login;//成功从userLoginMpnd跳转过来标志，yes
			var filtervo = "";//页面查询条件
			var searchParam ="";//上一次的查询记录
		
			/*--若是客户人员，弹出虚拟工号确认界面  --*/
			var user = "";//用户数据
			var userType="";  					//	用户类型
			var userVirtualId="";				//  定义虚拟工号，先默认是空
			var token=sessionStorage.getItem("token");
			
			$scope.data = {
				checkFlag: false,
				checkType: 1,
		        checkTypes:[
		        ],
				policys: [
				]
			};
			
/*****************************************************初始化函数begin***********************************************************/			
			var timestamp=myFactory.getTimetamp();
			var sfis= "token-timestamp";
			var signStr="token="+token+"&timestamp="+timestamp;//encodeURI(name)
			var sign = myFactory.hex(signStr);
			//初始化时候先读取一下历史查询条件
			$http({
				method: 'GET',
				url: app.domain + 'pos/historyQuery.action',
				params: {'sign':sign,'sfis':sfis,'token':token,'timestamp':timestamp}
			}).then(function successCallback(response) {
				if (response.data.success) { //程序正常运行
					searchParam = response.data.data.searchParam;
					if(searchParam!=null && searchParam!=undefined && searchParam!=""){ //如果查询条件不为空
						//console.log(searchParam);
						$("#fchdrnum").val(searchParam.chdrnum);
						$("#fapnum").val(searchParam.apnum);
						$("#fcownm").val(searchParam.cownm);
						if(searchParam.chdrnum!=""){ //如果保单号不为空，默认以该方式初始化
							$("#checkTypeSelect").val('1');
							$scope.data.checkType = 1;
						}else if(searchParam.apnum!=""){
							$("#checkTypeSelect").val('2');
							$scope.data.checkType = 2;
						}else{
							$("#checkTypeSelect").val('3');
							$scope.data.checkType = 3;
						}
					}else{
						$("#checkTypeSelect").val('1');
						$scope.data.checkType = 1;
					}
				}
			}, function errorCallback(response) {
				//初始化查询上次查询条件失败不需要提醒，不会影响功能正常使用
			});
			
			//访问后台用户缓存信息，反馈到前台，冰根据类型执行相应操作
    		$http({
				method: 'GET',
				url: app.domain + 'user/restGetUserInfo.action'
			}).then(function successCallback(response) {
				if (response.data.success) {
					//缓存用户不为null时候才赋予，否则不做操作
					if(response.data.data!=null){
						user = response.data.data;
						userVirtualId = user.userVirtualId;
						userType = user.userType
					}

					//测试虚拟工号弹出框
					//userVirtualId="";
					//判断是否弹框
					//虚拟工号为空，且用户类型为"客户人员"时弹出虚拟工号对话框
					if( userVirtualId=="" ){
						userType="client"
						if( userType=="client" ){
							modal('open','overmodalOnMpndSearch','overwindowOnMpndSearch','closeX');
						}
					}
				}
			}, function errorCallback(response) {
				myFactory.alert("","查询用户信息失败!","info");
			});
			
/*****************************************************初始化函数end***********************************************************/
    		//更换查询方式
    		$scope.changeSelect = function(type){
				$scope.data.checkType = type;
			}
			//取消输入虚拟工号
			$scope.returnXyt = function(){
				var data = {
					sessionTimeOut : "No",
					exitSysPage : "2" //"1" 信易通首页  "2":“我”页面
				};
				setTimeout(function() {
					//cordova.sino.entryLocalSysFromOtherSys(function(){}, data);
					cordova.sino.entryLocalSys(function(){}, data);
				}, 350);
			}
			//虚拟工号校验	
    		$scope.checkXngh = function(){
    			myFactory.loadingShow();//显示加载圈
    			$("#errorMessgae").html("");
    			var xngh = $("#userVirtualId").val();
    			//先输入的虚拟工号格式进行校验
    			 if (xngh=="null" || xngh.trim()=="") {
    				myFactory.loadingHide();
    				$("#errorMessgae").html("请输入虚拟工号！");
    		        return;    
    		     }else if(xngh!=""){
    		        var Reg =/^[0-9]{8}$/;
    		        if (!Reg.test(xngh)) {
    		        	myFactory.loadingHide();
    		        	$("#errorMessgae").html("请输入8位数字！");
    		        	return; 
    		        }      	
    		     }
    			var timestamp=myFactory.getTimetamp();
 				var sfis= "userVirtualId-token-timestamp";
 				var signStr="userVirtualId="+xngh+"&token="+token+"&timestamp="+timestamp;//encodeURI(name)
 				var sign = myFactory.hex(signStr);
    			//校验完毕，提交后台校验
    			$http({
					method: 'GET',
					url: app.domain + 'user/restSaveVirtualId.action',
					params: {"userVirtualId":xngh,'sign':sign,'sfis':sfis,'token':token,'timestamp':timestamp}
				}).then(function successCallback(response) {
					if (response.data.success) {
						user = response.data.data.user;
						var isExis = response.data.data.isExis;
						if(isExis=="0"){
							myFactory.loadingHide();
							$("#errorMessgae").html("请输入正确的虚拟工号！");
						}else if(isExis=="1"){
							myFactory.loadingHide();
							modal('close','overmodalOnMpndSearch','overwindowOnMpndSearch','closeX');
						}
					}
					myFactory.loadingHide();
				}, function errorCallback(response) {
					myFactory.loadingHide();
					myFactory.alert("","提交校验失败，请稍后重试","info");
				});
    		}
			
			$scope.queryPolicyList = function(){
				myFactory.loadingShow();//显示加载圈
				//得到当前选择的方式
				var type = $scope.data.checkType;
				//得到数据
				var chdrnum = $("#fchdrnum").val().trim();
				var apnum = $("#fapnum").val().trim();
				var cownm = $("#fcownm").val().trim();
				
				if(type==1){
					apnum = "";
					cownm = "";
				}else if(type==2){
					chdrnum = "";
					cownm = "";
				}else if(type==3){
					chdrnum = "";
					apnum = "";
				}
				
				//校验部分，暂时跳过
				
				//封装数据
				var item = {
					"chdrnum":chdrnum,
					"apnum":apnum,
					"cownm":cownm
				}
				//提交后台
				var timestamp=myFactory.getTimetamp();
				var sfis= "token-timestamp";
				var signStr="token="+token+"&timestamp="+timestamp;//encodeURI(name)
				var sign = myFactory.hex(signStr);
    			$http({
					method: 'GET',
					url: app.domain + 'pos/pending.action',
					params: {"queryDataStr":item,'sign':sign,'sfis':sfis,'token':token,'timestamp':timestamp}
				}).then(function successCallback(response) {
					if (response.data.success) { //程序正常运行
						if(response.data.data.code == "-1"){  //返回-1说明查找不到用户
							myFactory.loadingHide();
							myFactory.alert("",response.data.data.message,"info");
							return false;
						}else{
							var data = response.data.data.modelMap;
							$scope.policyList = data.policyList;
							if($scope.policyList==""||$scope.policyList==null){
								myFactory.loadingHide();
								myFactory.alert("","无该查询条件下照会信息，请重新确认！","info");
								return false;
							}	
							filtervo = data.filterVo;
							myFactory.loadingHide();
							//myFactory.alert("","查询数据成功","info");
						}
					}else{//异常结束时
						myFactory.loadingHide();
						myFactory.alert("","查询失败，请稍后重试","info");
					}
				}, function errorCallback(response) { //访问失败时
					myFactory.loadingHide();
					myFactory.alert("","查询失败，请稍后重试","info");
				});
				
			}
			
			//回复，查询保单的详细照会信息
			$scope.huifu = function(index){
				myFactory.loadingShow();//显示加载圈
				//根据index，提取到相应的要查询的某一条数据，并封装数据提交后台
				console.log(index);
				
				var chdrnum = $("#chdrnum_"+index).val().trim();
				var	cntbranch = $("#cntbranch_"+index).val().trim();
				var	chdrcoy = $("#chdrcoy_"+index).val().trim();
				var	apnum = $("#apnum_"+index).val().trim();
				var	pndate = $("#pndate_"+index).val().trim();
				var	cownm = $("#cownm_"+index).val().trim();
				var	cownnum = $("#cownnum_"+index).val().trim();
				var	lifenm = $("#lifenm_"+index).val().trim();
				var	lifenum = $("#lifenum_"+index).val().trim();
				var	fromtyp = $("#fromtyp_"+index).val().trim();
				var	zduedate = $("#zduedate_"+index).val().trim();
				
				var policyInfo = {
					chdrnum : chdrnum,
					cntbranch : cntbranch,
					chdrcoy : chdrcoy,
					apnum : apnum,
					pndate : pndate,
					cownm : cownm,
					cownnum : cownnum,
					lifenm : lifenm,
					lifenum : lifenum,
					fromtyp : fromtyp,
					zduedate : zduedate
				}
				
				var timestamp=myFactory.getTimetamp();
				/*S2022041822029--PIC_双被保险人增额终身寿险产品系统需求 add by huanghk 20220816 start*/
				var sfis= "chdrnum-token-timestamp";
				var signStr="chdrnum="+chdrnum+"&token="+token+"&timestamp="+timestamp;//encodeURI(tbrName)
				var sign = myFactory.hex(signStr);
				myFactory.loadingShow();
				$http({
					method: 'GET',
					url: app.domain + 'query/checkLife2.action',
					params: {'chdrnum': chdrnum,'sign':sign,'sfis':sfis,'token':token,'timestamp':timestamp}
				}).then(function successCallback(response) {
					if (response.data.success) {
						if(response.data.data){
							myFactory.alert("","暂不支持双被保险人保单操作，请重新确认！","info");
						}else{
							var sfis= "token-timestamp";
							var signStr="token="+token+"&timestamp="+timestamp;//encodeURI(name)
							var sign = myFactory.hex(signStr);
			    			$http({
								method: 'GET',
								url: app.domain + 'pos/queryPolicyInfo.action',
								params: {"queryDataStr":policyInfo,'sign':sign,'sfis':sfis,'token':token,'timestamp':timestamp}
							}).then(function successCallback(response) {
								if (response.data.success) { //程序正常运行
									if(response.data.data.code == "-1"){  //返回-1说明查找不到用户
										//myFactory.loadingHide();
										myFactory.alert("",response.data.data.message,"info");
									}else{
										var data = response.data.data.modelMap;
										//myFactory.loadingHide();//取消转菊
										$ionicHistory.clearCache().then(function(){ 
											$state.go('presentdeliver_operate',{
												returnData:data
											});
										});
									}
								}else{//异常结束时
									//myFactory.loadingHide();
									myFactory.alert("","查询失败，请稍后重试","info");
								}
							}, function errorCallback(response) { //访问失败时
								//myFactory.loadingHide();
								myFactory.alert("","查询失败，请稍后重试","info");
							});
						}
					}
					myFactory.loadingHide();
				}, function errorCallback(response) {
					myFactory.alert("","查询数据失败!","info");
					myFactory.loadingHide();
				});
				/*S2022041822029--PIC_双被保险人增额终身寿险产品系统需求 add by huanghk 20220816 end*/
				
				
			}
			
			/**
			 * 只输入整数函授
			 */
			$scope.keyNumPress = function(){
				var keyCode = event.keyCode;
				if(keyCode>=48 && keyCode<=57){
					event.returnValue = true;
				}else{
					event.returnValue = false;
				}
			}
			
			//返回上一页
			$scope.goback = function(){
				myFactory.goback();
			}
			//退出移动保全
			$scope.exit = function(){
				myFactory.exitToMpnd();
			}
		}]);        
})(app)